package com.itheima.web.service.impl.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.service.pms.PmsBrandService;
import com.itheima.utils.PageResult;
import com.itheima.utils.ResultData;
import com.itheima.vo.pms.Brand;
import com.itheima.web.mapper.pms.PmsBrandMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, Brand> implements PmsBrandService {


    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    /**
     * 品牌添加
     * @param brand
     * @return
     */
    @Override
    public ResultData insert(Brand brand) {
        this.save(brand);
        return ResultData.ok("","");
    }

    /**
     * 批量删除品牌
     * @param ids
     * @return
     */
    @Override
    public ResultData removeIds(List<Integer> ids) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Brand::getId , ids);
        this.remove(wrapper);
        return ResultData.ok("删除成功", ids.size());
    }

    /**
     * 根据品牌名称分页获取品牌列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResultData listPage(String keyword, Integer pageNum, Integer pageSize) {
        Page<Brand> page = new Page<>(pageNum ,pageSize);
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(keyword != null , Brand::getName, keyword);
        this.page(page, wrapper);
        //return ResultData.ok("OK", PageResult.init(page.getCurrent() , pageNum.longValue() , pageSize.longValue() ,page.getRecords() ));
        PageResult result = PageResult.getPage(page);
        return ResultData.ok("",result);
    }

    /**
     * 批量更新厂家制造商状态
     * @param ids
     * @param factoryStatus
     */
    @Override
    public void updateFactoryStatus(List<Integer> ids, Integer factoryStatus) {
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Brand::getId , ids);

        List<Brand> list = this.list(wrapper);
        for (Brand brand : list) {
            brand.setFactoryStatus(factoryStatus);
            this.updateById(brand);
        }
    }

    /**
     * 批量更新显示状态
     * @param ids
     * @param showStatus
     */
    @Override
    public void updateShowStatus(List<Integer> ids, Integer showStatus) {
        //条件构造器
        LambdaQueryWrapper<Brand> lqw=new LambdaQueryWrapper<>();
        lqw.in(Brand::getId,ids);
        List<Brand> list = this.list(lqw);
        for (Brand brand : list) {
            brand.setShowStatus(showStatus);
            this.updateById(brand);
        }
    }
}
